#include <stdio.h>
#include <stdlib.h>
#define DataType char
#define MAXLENGTH 20
#include "conjuction.h"


//--------A Infix to Polish Expression Converter-------------------------------//  
int convertInfixPolish (char *infix, char *polish){
    PSeqStack ps_operator=createEmptyStack_seq(MAXLENGTH);
    int continuity=0,blank_flag=0;
    char temp, temp_top=0;
    int i,j = 0;
//--------LOOP STARTS!-------------------------------------------------//   
    for(i=0;infix[i]!=';';i++){                                
          temp=infix[i];
//--------Condition: Blank-------------------------------------------------// 
          if (temp==' '){
            if (continuity==1){ 
                blank_flag=1;
                }          
          } 
//--------Condition: Digit-------------------------------------------------//           
          else if (temp>='0'&&temp<='9'){
          	polish[j]=temp;
          	j=j+1;
          	continuity=1;
          	if (blank_flag==1) return 0;
          }
//--------Condition: "+", "-"------------------------------------------------//             
          else if (temp=='+'||temp=='-'){
               blank_flag=0;
          	if(continuity==1){
          		polish[j]=' ';
          		j=j+1;
          	  continuity=0;
            }
            if (isEmptyStack_seq(ps_operator)==0){
            	temp_top=top_seq(ps_operator);
            	if (temp_top!='('&&temp_top!=')'){
            		polish[j]=temp_top;
          	        j=j+1;
          	        polish[j]=' ';
          	        j=j+1;
            		pop_seq(ps_operator);
            		}
            }
           	push_seq(ps_operator,temp);
           	temp_top=top_seq(ps_operator);
          }
//--------Condition: "*", "/"------------------------------------------------//            
          else if (temp=='*'||temp=='/'){
               blank_flag=0;
          	if(continuity==1){
          		polish[j]=' ';
          		j=j+1;
          	  continuity=0;
            }
            if (isEmptyStack_seq(ps_operator)==0){
            	temp_top=top_seq(ps_operator);
            	if (temp_top!='('&&temp_top!=')'&&temp_top!='+'&&temp_top!='-'){
            		polish[j]=temp_top;
          	    j=j+1;
            		pop_seq(ps_operator);
            		}
            }
           	push_seq(ps_operator,temp);
         }
//--------Condition: "("---------------------------------------------------//            
          else if (temp=='('){
               blank_flag=0;
          	if(continuity==1){
          		return 0;
          		break;
            }
           	push_seq(ps_operator,temp);
         }          
//--------Condition: ")"---------------------------------------------------//
          else if (temp==')'){
               blank_flag=0;
          	if(continuity==1){
          		polish[j]=' ';
          		j=j+1;
          	  continuity=0;
            }
          	temp_top=top_seq(ps_operator);
            while(isEmptyStack_seq(ps_operator)==0&&temp_top!='('){
            	polish[j]=top_seq(ps_operator);
            	j=j+1;
            	pop_seq(ps_operator);
            	temp_top=top_seq(ps_operator);
            	polish[j]=' ';
            	j=j+1;
            	}
           	if (temp_top=='(') pop_seq(ps_operator);
            else { 
            	polish[0]='b';
            	return 0;
            	break;
             }
         }
//--------Other Conditions---------------------------------------------------//         
        else {
        	free(ps_operator);
        	return 0;
         }  
    }
//--------Breaks the Loop---------------------------------------------------// 
   if(continuity==1){
          		polish[j]=' ';
          		j=j+1;
          	  continuity=0;
            }
   temp_top=top_seq(ps_operator);
    while(isEmptyStack_seq(ps_operator)==0&&temp_top!='('){
            	temp_top=top_seq(ps_operator);
            	polish[j]=temp_top;
            	j=j+1;
            	polish[j]=' ';
            	j=j+1;
            	pop_seq(ps_operator);
            	}
    free(ps_operator);
    polish[j]='\0';
    if (temp_top=='('){ 
            	polish[0]='b';
            	return 0;
    }
    else return 1;
    }
    
